<?php
/*
	本插件：$this->model('api')->方法名();
	其他地方调用：model('jy_hnapay/api')->方法名();
*/
namespace jy_hnapay;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	private $version = "2.0";               // 版本号
    private $merId;                         // 商户ID
    private $submitTime;                    // 请求提交时间(格式：yyyyMMddHHmmss)
    private $merOrderId;                    // 商户订单号
    private $signType = "1";                // 签名类型(固定1：RSA)
    private $merAttach = "1";               // 附加数据(可为空)
    private $charset = "1";                 // 编码方式(固定1：UTF8)
    private $merUserIp = "192.168.1.1";     // 商户用户IP
    private $hnapayOrderId;                 // 新生订单号(支付请求下单返回)

    private $url05 = "https://gateway.hnapay.com/exp/payRequest.do";
    private $url06 = "https://gateway.hnapay.com/exp/paySms.do";
    private $url07 = "https://gateway.hnapay.com/exp/payConfirm.do";

    public function __construct(){
        $basic=plugin('jy_hnapay')->plugin_setting('basic');
		if(!$basic['merid']){
			exi('商户ID未填！','error');
		}
		$this->submitTime = date('YmdHms');
        $this->merId = $basic['merid'];
        $this->merOrderId = "EXPPAY" . "_" . date('YmdHms').floor(microtime()*1000);
    }
	public function pay($order=array()){
        // 调用支付请求下单
        try {
            $param05 = $this->genExp05Data($order);
            $resMap05 =  model('jy_hnapay/model')->request($this->url05,$param05);
            //返回结果
            if ("0000" != $resMap05['resultCode']){
                exi("调用支付请求下单失败:".$resMap05['errorCode'].$resMap05['errorMsg'],'error');
                return;
            }
            //验签
            $verify =model('jy_hnapay/model')->verify("EXP05", $this->genExp05Data(), $resMap05['signValue']);
            $this->hnapayOrderId = $resMap05['hnapayOrderId'];
            if(!$verify) {
                 exi("调用支付请求下单验签失败".$verify['errorCode'].$verify['errorMsg'],'error');
                return;
            }
        } catch (Exception $e){
            echo "exception:".$e->getMessage();
        }

        //调用支付获取短信
        try{
            $param06 = $this->genExp06Data();
            $resMap06 =model('jy_hnapay/model')->request($this->url06,$param06);
			
            //返回结果
            if ("0000" != $resMap06['resultCode']){
                exi("调用支付获取短信失败:" . $resMap06['errorCode'].$resMap06['errorMsg'],'error');
                return;
            }
            //验签
            $verify =model('jy_hnapay/model')->verify("EXP06", $this->genExp06Data(), $resMap06['signValue']);
            $this->hnapayOrderId = $resMap06['hnapayOrderId'];
            if(!$verify) {
                exi("调用支付获取短信失败".$verify['errorCode'].$verify['errorMsg'],'error');
                return;
            }
        } catch (Exception $e){
            exi("exception:".$e->getMessage(),'error');
        }
		return  $this->hnapayOrderId;
    }
	public function checkpay($order=array()){
        //请输入短信验证码
        try{
            $param07 = $this->genExp07Data();
            $res07 =model('jy_hnapay/model')->post($this->url07,$param07);
            $resMap07 = json_decode($res07, true);
            //返回结果
            if ("0000" != $resMap07['resultCode']){
                exi("调用支付获取短信失败:" . $resMap07['errorCode'],'error');
                return;
            }
            //验签
            $verify =model('jy_hnapay/model')->verify("EXP07", $this->genExp07Data(), $resMap07['signValue']);
            if(!$verify) {
                exi("调用支付获取短信失败",'error');
                return;
            }
        } catch (Exception $e){
            exi("exception:".$e->getMessage(),'error');
        }
		return  true;
    }
	/**
     * 生成支付下单请求参数
     * @return array
     */
    private function genExp05Data($log=array())
    {
        $params = array();
        // 报文密文参数
        $params['tranAmt'] = $log['fee'];                    // 支付金额
        $params['payType'] = "1";                        // 支付银行卡类型+支付银行简码
        $params['cardType'] = "1";                       // 借记卡
        $params['bankCode'] = $log['bankcode'];                     // 支付银行简码
        $params['cardNo'] = "";                          // 支付银行卡卡号
        $params['bizProtocolNo'] = "";                   // 用户业务协议号
        $params['payProtocolNo'] = "";                   // 支付协议号
        $params['frontUrl'] = "";                        // 商户前台跳转地址
        $params['notifyUrl'] = SITEROOT .'addons/jy_hnapay/notify.php';  // 商户异步通知地址
        $params['orderExpireTime'] = "";                 // 订单过期时长
        $params['merUserIp'] = $this->merUserIp;         // 商户用户IP
        $params['riskExpand'] = "";                      // 风控扩展信息
        $params['goodsInfo'] = $log['goodsinfo'];           // 商品信息

        // 报文明文
        $params['version'] = $this->version;
        $params['tranCode'] = "EXP05";
        $params['merId'] = $this->merId;
        $params['merOrderId'] = $log['paylogtid'];
        $params['submitTime'] = $this->submitTime;

        // 报文密文加密(需要加密的字段参见接口说明)
        $params['msgCiphertext'] = model('jy_hnapay/model')->encrypt("EXP05", $params);
        $params['signType'] = $this->signType;
        $params['merAttach'] = $this->merAttach;
        $params['charset'] = $this->charset;
        try {
            // 签名(要签名的字段参见网关接口文档)
            $params['signValue'] = model('jy_hnapay/model')->sign("EXP05", $params);
        } catch (Exception $e) {
            echo "exception:".$e->getMessage();
            $params['signValue'] = "";
        }
        return $params;
    }

    private function genExp06Data($log=array()){
        $params['hnapayOrderId'] = $this->hnapayOrderId;     // 新生订单号

        // 根据支付请求下单接口返回的支付要素集合设置参数，
        //不需要的支付要素设置为空字符串("")
        $params['cardNo'] = $log['cardno'];           // 支付银行卡卡号
        $params['holderName'] = $log['name'];                       // 持卡人姓名
        $params['cardAvailableDate'] = "";                   // 信用卡有效期
        $params['cvv2'] = "";                                // 信用卡CVV2
        $params['mobileNo'] = $log['mobile'];                 // 银行签约手机号
        $params['identityType'] = "01";                      // 证件类型，暂仅支持01:身份证
        $params['identityCode'] = $log['code'];      // 证件号码
        $params['merUserId'] = $log['mid'];                    // 商户用户ID
        $params['merUserIp'] = $this->merUserIp;             // 商户用户IP

        // 报文明文
        $params['version'] = $this->version;
        $params['tranCode'] = "EXP06";
        $params['merId'] = $this->merId;
        $params['merOrderId'] = $log['paylogtid'];
        $params['submitTime'] = date('YmdHms');

        // 报文密文加密(需要加密的字段参见接口说明)
        $params['msgCiphertext'] = model('jy_hnapay/model')->encrypt("EXP06", $params);
        $params['signType'] = $this->signType;
        $params['merAttach'] = $this->merAttach;
        $params['charset'] = $this->charset;

        try {
            // 签名(要签名的字段参见网关接口文档)
            $params['signValue'] = model('jy_hnapay/model')->sign("EXP06", $params);
        } catch (Exception $e) {
            echo "exception:".$e->getMessage();
            $params['signValue'] = "";
        }
        return $params;
    }

    private function genExp07Data($log=array())
    {
        $params['hnapayOrderId'] = $log['hnapayorderid'];     // 新生订单号
        $params['smsCode'] = $log['sms_code'];                       // 短信验证码
        $params['merUserIp'] = $this->merUserIp;             // 商户用户IP
        // 报文明文
        $params['version'] = $this->version;
        $params['tranCode'] = "EXP07";
        $params['merId'] = $this->merId;
        $params['merOrderId'] = $log['paylogtid'];
        $params['submitTime'] = date('YmdHms');

        // 报文密文加密(需要加密的字段参见接口说明)
        $params['msgCiphertext'] = model('jy_hnapay/model')->encrypt("EXP07", $params);
        $params['signType'] = $this->signType;
        $params['merAttach'] = $this->merAttach;
        $params['charset'] = $this->charset;

        try {
            // 签名(要签名的字段参见网关接口文档)
            $params['signValue'] = model('jy_hnapay/model')->sign("EXP07", $params);
        } catch (Exception $e) {
            echo "exception:".$e->getMessage();
            $params['signValue'] = "";
        }
        return $params;
    }
}
?>